#!/bin/bash
#
# detect-language returns a string indicating the image repository to use as a base
# image for this source repository. Return "docker.io/*/*" for Docker images, a two
# segment entry for a local image repository, or a single segment name to search
# in the current namespace. Set a tag to qualify the version - e.g. "ruby:1.9.3",
# "nodejs:0.10".
#

set -o errexit
set -o nounset
set -o pipefail

function has {
  [[ -n $(git ls-tree --full-name --name-only HEAD ${@:1}) ]]
}
function hasglob {
  git ls-tree --full-name --name-only HEAD | while read NAME; do
    [[ "$NAME" == $1 ]] && return 0
  done
}
function key {
  git config --local --get "${1}"
}

prefix=${PREFIX:-openshift/}

if has Gemfile Rakefile config.ru; then
  echo "${prefix}ruby"
  exit 0
fi

if has pom.xml; then
  echo "${prefix}wildfly"
  exit 0
fi

if has package.json app.json; then
  echo "${prefix}nodejs"
  exit 0
fi

if has index.php composer.json; then
  echo "${prefix}php"
  exit 0
fi

if has requirements.txt setup.py; then
  echo "${prefix}python"
  exit 0
fi

if has index.pl cpanfile; then
  echo "${prefix}perl"
  exit 0
fi

if has build.sbt; then
  echo "${prefix}scala"
  exit 0
fi

if has project.json || hasglob '*.csproj'; then
  echo "${prefix}dotnet"
  exit 0
fi

if has main.go Godeps; then
  echo "${prefix}golang"
  exit 0
fi

exit 1
